/*-
 * CD.java
 *
 * $LastChangedBy: stein $
 * $LastChangedRevision: 98 $
 * $LastChangedDate: 2007-02-20 15:18:26 +0000 (Tue, 20 Feb 2007) $
 * $HeadURL: svn://localhost/SMS/src/com/googlecode/attention/sms/pdu/CD.java $
 *
 * Copyright (c) 2007, Stein Gunnar Bakkeby
 * All rights reserved.
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 *     * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 *     * Neither the name of Stein Gunnar Bakkeby nor the names of its
 *       contributors may be used to endorse or promote products derived
 *       from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS "AS IS" AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
package com.googlecode.attention.sms.pdu;

import com.googlecode.attention.util.Convert;
import com.googlecode.attention.util.IntArrayBuilder;

/**
 * TP-Command-Data (TP-CD).
 * <BR>
 * <BR>The TP-Command-Data field contains data relating to the operation
 * requested by the MS which is to be performed at the SC. The maximum length
 * of this field is 157 octets. The usage and provision of the optional
 * TP-Command-Data field shall be determined by the function selected by
 * the TP-Command-Type field.
 *
 * @author Stein Gunnar Bakkeby
 */
public class CD extends IntArrayBuilder {

  private static final long serialVersionUID = -9023662442967751903L;

  /** Max length of command-data field, in octets. */
  public static final int MAX_SPACE_COMMAND_DATA = 157; // octets
  /** Max guaranteed length of command-data field (DA = 2 octets). */
  public static final int MAX_GUARANTEED_SPACE   = 146; // octets
  
  /**
   * Constructor, creates the Command-Data field.
   */
  public CD() {
    super();
  }
  
  /**
   * Constructor, creates the Command-Data field.
   * 
   * @param cd The raw data of the CD field (excluding Command-Data-Length)
   */
  public CD(final int[] cd) {
    super(cd);
  }
  
  /**
   * This will calculate the space left.
   * 
   * @return MAX_SPACE_COMMAND_DATA - cdl
   */
  public int getSpaceLeft() {
    return MAX_SPACE_COMMAND_DATA - endpoint;
  }
  
  /**
   * This will calculate the guaranteed space left.
   * 
   * @return MAX_GUARANTEED_SPACE - cdl
   */
  public int getGuaranteedSpaceLeft() {
    return MAX_GUARANTEED_SPACE - endpoint;
  }
  
  /**
   * This will return the space used.
   * 
   * @return Command-Data-Length
   */
  public int getCDL() {
    return endpoint;
  }
  
  /**
   * @return Command data as a string
   */
  public String toString() {
    return Convert.intToString(this);
  }
}